﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MicroDal;
using System.Data.Common;

namespace MicroDalTests
{
    [TestClass]
    public class DynamicMapperTest
    {
        [TestMethod]
        public void ShouldMapColumnsToProperties()
        {
            Func<DbDataReader, dynamic> sut = DynamicMapper.Map;
            var r = new TestDbReader("Id", 1, "Name", "Første navn");

            dynamic actual = sut.Invoke(r);

            Assert.AreEqual(1, actual.Id);
            Assert.AreEqual("Første navn", actual.Name);
        }

        [TestMethod]
        public void ShouldMapColumnsToPropertiesValueDbNull()
        {
            Func<DbDataReader, dynamic> sut = DynamicMapper.Map;
            var r = new TestDbReader("Id", 1, "Name", DBNull.Value);

            dynamic actual = sut.Invoke(r);

            Assert.AreEqual(1, actual.Id);
            Assert.IsNull(actual.Name);
        }

        [TestMethod]
        public void ShouldMapColumnsToPropertiesInCamelCase()
        {
            Func<DbDataReader, dynamic> sut = DynamicMapper.Map;
            var r = new TestDbReader("Id", 1, "Name", "Første navn");

            dynamic actual = sut.Invoke(r);

            Assert.AreEqual(1, actual.id);
            Assert.AreEqual("Første navn", actual.name);
        }

        [TestMethod]
        public void ShouldBeAbleToModifyPropertyAfterInitialization()
        {
            Func<DbDataReader, dynamic> sut = DynamicMapper.Map;
            var r = new TestDbReader("Id", 1, "Name", "Første navn");

            dynamic actual = sut.Invoke(r);
            actual.id = 23;

            Assert.AreEqual(23, actual.id);
        }
    }
}
